<?php  
    //UserTools.class.php  
      
    require_once 'user.class.php';  
	require_once 'DB.class.php';  
      
    class UserTools {  
     
    //Log the user in. First checks to see if the  
	//username and password match a row in the database.  
    //If it is successful, set the session variables  
    //and store the user object within.  
        public function login($username, $password)  
        {  

                $hashedPassword = md5($password);  
                $result = mysql_query("SELECT * FROM tblmember WHERE email = '$username' AND password = '$hashedPassword'");  

                if(mysql_num_rows($result) == 1)  
                {  
                        $_SESSION["user"] = serialize(new User(mysql_fetch_assoc($result)));  
                        $_SESSION["login_time"] = time();  
                        $_SESSION["logged_in"] = 1;  
                        return true;  
                }else{  
                        return false;  
                }  
        }  
    
    //Log the user out. Destroy the session variables.  
        public function logout() {  
            unset($_SESSION['user']);  
            unset($_SESSION['login_time']);  
            unset($_SESSION['logged_in']);  
            session_destroy();  
        }  
    
    //Check to see if a username exists.  
    //This is called during registration to make sure all user names are unique.  
        public function checkUsernameExists($username) {  
                $result = mysql_query("select memid from tblmember where email='$username'");  
                if(mysql_num_rows($result) == 0)  
                {  
                        return false;  
                }else{  
                        return true;  
                }  
        }  
     
       //get a user  
       //returns a User object. Takes the users id as an input  
        public function get($id)  
        {  
                $db = new DBBase();  
                $result = $db->select('tblmember', "memid = $id");    
                return new User($result);  
        }            

        //get country
        public function get_areas()
        {
            $db = new DBBase();  
            $result = $db->selectall('tblcountry',"country_name"); 
            return new User($result);
        }
        
        //get bidhistory
        public function getbidhistory($memid)
        {
            $result = mysql_query("select tblproduct.pro_name,tblbidhistory.bid_price from tblproduct,tblbidhistory where tblproduct.pro_id = tblbidhistory.pro_id and tblbidhistory.memid = '$memid'");
            $data = array();
            while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) { 
                $data[] = $row;  
            }
            return $data;
        }
        //get bidhistory
        public function getAvartaByMemId($memid)
        {
			$db = new DBBase();  
			$db->connect();
            $result = mysql_query("select avarta from tblmember where memid = '$memid'");
            $row = mysql_fetch_assoc($result, MYSQL_ASSOC);
            return $row;
        }
        //won auction
        public function wonAution($memid)
        {
            $result = mysql_query("select tblproduct.pro_name,tblmemberwon.soldfor,tblmemberwon.sold_date,tblmemberwon.bid_used from tblmemberwon,tblproduct where tblproduct.pro_id = tblmemberwon.pro_id and tblmemberwon.memid = '$memid'");
            $data = array();
            while ($row = mysql_fetch_assoc($result, MYSQL_ASSOC)) { 
                $data[] = $row;  
            }
            return $data;
        }
        
        //check password old
        public function checkPassExists($pass,$memid) {  
				$hashedPassword = md5($pass);
                $result = mysql_query("select memid from tblmember where password = '$hashedPassword' and memid = '$memid'");  
		$count = mysql_num_rows($result);
                if($count == 0)  
                {  
                        return false;  
                }else{  
                        return true;  
                }  
        }  
        
        public function countAllproduct(){
            $db = new DBBase();
            $db->connect();
            $result = mysql_query("select * from tblproduct where status = 1");  
            $count = mysql_num_rows($result);
            return (int)$count;
        }
        
        public function totalBid($pro_id){
            $db = new DBBase();
            $db->connect();
            $result = mysql_query("select * from tblproduct where pro_id = '$pro_id'");  
            $count = mysql_num_rows($result);
            return (int)$count;
        }

    }  

?>  